﻿//Tzi Yong

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Tzync
{
    public partial class MatchProfileUIForm : Form
    {
        private SyncTool syncTool;
        private Profile profile;
        private String rootAddress; //address of selected folder
        private bool isCompleted;   //whether folder has been selected

        public MatchProfileUIForm(SyncTool newSyncTool, bool completed)
        {
            syncTool = newSyncTool;
            profile = syncTool.GetProfile();
            isCompleted = completed;
            InitializeComponent();
        }

        private void MatchProfileUIForm_Load(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            profileLabel.Text = "Completing Profile: " + profile.ProfileName;
            browseBtnToolTip.SetToolTip(browseBtn, "Click here to browse for a folder to sync");
            applyBtnToolTip.SetToolTip(applyBtn, "Click here to complete the given profile using the information above");
            cancelBtnToolTip.SetToolTip(cancelBtn, "Click here to cancel and return to Profiles");
        }

        private void MatchProfileUIForm_Shown(object sender, EventArgs e)
        {
            syncFolderTB.Focus();
        }

        private void browseBtn_Click(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            DialogResult result = syncFolderBrowserDialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                rootAddress = syncFolderBrowserDialog.SelectedPath;
                syncFolderTB.Text = rootAddress;
            }
            else
                return;
        }

        private void MatchProfileUIForm_DragEnter(object sender, DragEventArgs e)
        {
            infoLabel.Text = "";
            //only allow folders to be dropped here
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
            {
                String[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                if (Directory.Exists(files[0]))
                    e.Effect = DragDropEffects.All;
            }
        }

        private void MatchProfileUIForm_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            rootAddress = files[0];
            syncFolderTB.Text = rootAddress;
        }

        private void applyBtn_Click(object sender, EventArgs e)
        {
            
            if (!Directory.Exists(syncFolderTB.Text) || syncFolderTB.Text.Equals(""))
            {
                infoLabel.Text = "Please enter a valid folder to sync!";
                syncFolderTB.Focus();
                return;
            }

            infoLabel.Text = "Syncing in progress...";
            Cursor.Current = Cursors.WaitCursor;
            ModifyProfile();
            Cursor.Current = Cursors.Default;
            isCompleted = true;
            this.Close();
        }

        private void syncFolderTB_TextChanged(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            syncFolderTB.Focus();
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ModifyProfile()
        {
            syncTool.ModifyProfile(profile, rootAddress);   //completes profile
            syncTool.LoadMetadata();    //load metadata           
            isCompleted = true;
        }

        public bool IsCompleted
        {
            get { return isCompleted; }
            set { isCompleted = value; }
        }
    }
}
